Ext.define('Seq.app.CrudController', {
    extend: 'Seq.app.BaseController',

    /**
     * 复制一条记录的内容，用于编辑修改，，否则无法撤销修改
     */
    duplicate(record) {
        var duplicate = Ext.create(record.store.model, Ext.apply({}, record.data));
        duplicate.store = record.store;
        return duplicate;
    },

    onOk(button) {
        var me = this, win = button.up('window'), vm = win.getViewModel();
        var form = win.items.items[0]; // TODO 下一步处理tab问题
        var record = vm.get('record'), store = record.store;
        me.beforeSave && Ext.callback(me.beforeSave, me, [record, win]);
        Seq.Ajax.submit(form, {
            scope: me,
            mask: win,
            fnSuccess(opts, result) {
                me.afterSave(result, record, opts);
                win.close();
            }
        }, record);
    },

    afterSave: function (result, record, opts) {
        Ext.apply(record.data, result.data); // 空节点不输出，导致页面无法删除
        opts.scope.merge(record.store, record);
    },

    onCancel(button) {
        var win = button.up('window');
        win.close();
    },

    getGrid(btn) {
        return btn.up && (btn.up('grid') || btn.up('treepanel')) || this.getView();
    },

    showWin(detail, fnOk, model, config) {
        var me = this;
        config = Ext.apply({
            xtype: 'detailwin',
            renderTo: Ext.getBody(),
            viewModel: model || true,
            bind: {
                title: '{title}'
            },
            items: {
                xtype: detail
            },
            buttons: [{
                text: '确定',
                ui: 'soft-green',
                iconCls: 'x-fa fa-check fa-lg',
                reference: 'btnOk',
                scope: me,
                handler: fnOk
            }, {
                ui: 'gray',
                text: '取消',
                scope: me,
                reference: 'btnCancel',
                handler: me.onCancel
            }]

        }, config);
        var win = Ext.widget(config);
        return win;
    },

    showDetail(detail, record, isEdit, config) {
        var me = this, dup = me.duplicate(record);
        dup.parentNode = record.parentNode; // 复制不了，只能单独赋值
        var win = me.showWin(detail, me.onOk, {
            data: {
                record: dup,
                title: dup.getText(),
                isEdit: isEdit
            }
        }, config);
        me.afterDetail(dup, win);
        win.show();
    },

    onCellDblClick(view, td, cellIndex, record, tr, rowIndex, e, eOpts) {
        var grid = view.ownerCt, headerCt = grid.getHeaderContainer();
        var column = headerCt.getHeaderAtIndex(cellIndex);
        if (column.isXType('actioncolumn') || column.isXType('checkcolumn') || column.config.editor) { // 所有的check框，都不能产生双击效果
            return;
        }
        this.showDetail(grid.detail, record, true);
    },

    onDuplicateActionClick(view, rowIndex, colIndex, item, e, record, row) {
        var me = this, grid = view.ownerCt, duplicate = me.duplicate(record);
        delete duplicate.data.id;
        me.showDetail(grid.detail, duplicate, false);
    },

    onDeleteClick(btn, e) {
        var me = this, grid = me.getGrid(btn), records = grid.getSelection();
        Seq.Ajax.drop(records, {
            mask: grid,
            scope: me,
            beforeDrop: me.beforeDrop,
            afterDrop: me.afterDrop
        });
    },

    onEditActionClick(view, rowIndex, colIndex, item, e, record, row) {
        var grid = view.ownerCt;
        this.showDetail(grid.detail, record, true);
    },

    onDeleteActionClick(view, rowIndex, colIndex, item, e, record, row) {
        let me = this, records = [];
        records.push(record);
        Seq.Ajax.drop(records, {
            mask: view,
            scope: me,
            beforeDrop: me.beforeDrop,
            afterDrop: me.afterDrop
        });
    },

    afterDrop(option, records) {
        // TODO 删除后的操作
    },

    afterDetail(record, win) {
        let me = this, cmps = win.query('component[store]'), i, len = cmps.length;
        for (i = 0; i < len; i++) {
            let cmp = cmps[i], stub = cmp.bind && cmp.bind.store && cmp.bind.store.stub;
            let name = stub && stub.name, store = name && me.getViewModel().get(name);
            if (store) {
                win.getViewModel().set(name, store);
                store.isLoaded() || store.isLoading() || store.reload();
            }
        }
    }

});